package com.epoint.service.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 国泰新点自己封装的jdbc
 *
 * @author wangchao
 *
 */
public class CommonDao {

    private Connection conn;

    private CommonDao() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String dburl = "jdbc:mysql://localhost:3306/test";
            conn = DriverManager.getConnection(dburl, "root", "11111");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static CommonDao instance;

    public static CommonDao getInstance() {
        if (instance == null)
            instance = new CommonDao();
        return instance;
    }

    public <T> List<T> findList(String sql, Class<T> cls) {

        try {
            Statement stat = conn.createStatement();
            ResultSet rst = stat.executeQuery(sql);
            List<T> relst = new ArrayList<T>();

            while (rst.next()) {
                T obj = cls.newInstance();
                HashMap<String, Object> rec= (HashMap<String, Object>) obj;

                ResultSetMetaData rsmd = rst.getMetaData();
                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    rec.put(rsmd.getColumnName(i + 1).toLowerCase(), rst.getObject(rsmd.getColumnName(i + 1)));
                }
                relst.add(obj);
            }

            rst.close();
            stat.close();

            return relst;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public <T> T find(String sql, Class<T> clazz) {
        T result = null;
        List<T> list = findList(sql, clazz);
        if (list != null && list.size() > 0) {
            result = list.get(0);
        }
        return result;
    }

}
